package com.coocaa.app.lib.cleaner.deleter.pkgcache.impl;

import android.content.Context;
import android.content.pm.IPackageDataObserver;
import android.content.pm.PackageManager;
import android.os.RemoteException;

import com.coocaa.app.lib.cleaner.deleter.pkgcache.PackageCacheDeleterFactory;
import com.coocaa.app.lib.cleaner.mgr.DataHolder;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created by lu on 17-4-13.
 */

public class PackageCacheDeleterImpl implements PackageCacheDeleterFactory.PackageCacheDeleter {
    private void deletaCache(PackageManager pm, String packageName) throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
        Method deleteApplication = pm.getClass().getMethod(
                "deleteApplicationCacheFiles", String.class,
                IPackageDataObserver.class);
        deleteApplication.invoke(pm, packageName,
                new IPackageDataObserver.Stub() {
                    @Override
                    public void onRemoveCompleted(String packageName,
                                                  boolean succeeded) throws RemoteException {
                        latch.countDown();
                    }
                });
        latch.await();
    }

    private synchronized void start(final PackageManager pm, final List<DataHolder.SinglePackageCache> datas, final PackageCacheDeleterListener listener) {
        DeleterThread.post(new Runnable() {
            private void deleteOne(DataHolder.SinglePackageCache data) {
                try {
                    if (listener != null)
                        listener.onPackageCacheDeleterStart(data.packageName);
                    deletaCache(pm, data.packageName);
                    if (listener != null)
                        listener.onPackageCacheDeleterEnd(data.packageName, true, data.size);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (listener != null)
                        listener.onPackageCacheDeleterEnd(data.packageName, false, data.size);
                }
                if (holder != null)
                    holder.removePackageCache(data);
            }

            @Override
            public void run() {
                List<DataHolder.SinglePackageCache> _datas = new ArrayList<DataHolder.SinglePackageCache>(datas);
                if (listener != null)
                    listener.onPackageCacheDeleterAllStart();
                for (DataHolder.SinglePackageCache data : _datas) {
                    deleteOne(data);
                }
                if (listener != null)
                    listener.onPackageCacheDeleterAllEnd();
            }
        });
    }

    private DataHolder holder;

    @Override
    public void init(DataHolder holder) {
        this.holder = holder;
    }

    @Override
    public void delete(final Context context, final DataHolder.SinglePackageCache cache, final PackageCacheDeleterListener listener) {
        List<DataHolder.SinglePackageCache> caches = new ArrayList<DataHolder.SinglePackageCache>();
        caches.add(cache);
        delete(context, caches, listener);
    }

    @Override
    public void delete(final Context context, final List<DataHolder.SinglePackageCache> caches, final PackageCacheDeleterListener listener) {
        start(context.getPackageManager(), caches, listener);
    }
}
